package me.zingon.controller;

import java.awt.List;
import java.io.IOException;
import java.io.OutputStream;
import java.net.URISyntaxException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.http.client.ClientProtocolException;
import org.apache.http.util.EntityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.client.RestTemplate;
import org.springframework.web.servlet.ModelAndView;

import me.zingon.dao.ClazzDao;
import me.zingon.dao.TradDao;
import me.zingon.dao.UserDao;
import me.zingon.model.User;
import me.zingon.service.NetService;
import me.zingon.service.UserService;

@Controller
@Scope("session")
@RequestMapping("/hello")
public class TestController {
	
	@Autowired
	ClazzDao clazzDao;
	
	@Autowired
	TradDao tradDao;
	
	@Autowired
	UserDao userDao;
	
	@Autowired
	NetService netService;
	
	@Autowired
	UserService userService;
	
	@RequestMapping("/get") 
	public void myGet(@RequestParam("url")String url,@RequestParam(value="params",required=false,defaultValue="")String params,HttpServletRequest req,HttpServletResponse res) throws ClientProtocolException, URISyntaxException, IOException{
		Map<String,String> map=new HashMap<String,String>();
		String[] paraml=params.split(",");
		for (String param : paraml) {
			String[] p=param.split(":");
			if(p.length==2)
				map.put(p[0], p[1]);
		} 
		OutputStream out=res.getOutputStream();
		out.write(EntityUtils.toString(netService.myGet(url, map).getEntity()).getBytes());
		out.flush();
		out.close();
	}

	@RequestMapping("/getYzm") 
	public void myGet(HttpServletResponse res) throws ClientProtocolException, URISyntaxException, IOException{
		netService.myGetYzm(res); 
	}
	
	@RequestMapping("/mvc/{arg}")
	@ResponseBody
	public Object myMvc(@PathVariable("arg")String arg){
		RestTemplate rt=new RestTemplate(); 
		return "this is "+arg;
	}
	
	@RequestMapping("/a")
	@ResponseBody
	public Object a(){
		Map<String,String> map=new HashMap<String,String>();
		map.put("msg", "this is msg");
		return map;
	}
	
	@RequestMapping("/b")
	public ModelAndView b(ModelAndView model){
		model.setViewName("/home"); 
		model.addObject("trad", "TRAD");
		model.addObject("clazz","CLAZZ");
		return model;
	}
	
	@RequestMapping("/getUserByStuid")
	@ResponseBody
	public Object getUserByStuid(){
		return new User();
	}
	
	@RequestMapping("/modelAndView")
	public ModelAndView testMAV(ModelAndView model, String a){
		Map<String,Object> map=new HashMap<String, Object>();
		map.put("msg", "this is msg");
		model.addAllObjects(map);
		model.setViewName("/b");
		return model;
	}
	
	@RequestMapping("/table")
	@ResponseBody
	public Object table(){
		ArrayList<User> list=new ArrayList<User>();
		list.add(new User()); 
		Thread a=new Thread(); 
		return list;
	}
	
	
}
;
